﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EnglishAuction.BUS
{
    public class LoaiSanPhamBUS
    {
        public static LoaiSanPham Get(String tenLoai)
        {
            LoaiSanPham loaiSanPham = null;

            using (var context = new EnglishAuctionEntities())
            {
                var result = context.LoaiSanPhams.Where(o => o.MaLoai.Equals(tenLoai) == true).FirstOrDefault();
                if (result != null)
                    loaiSanPham = result;
            }

            return null;
        }

        public static bool Delete(int id)
        {
            bool result = true;
            try
            {
                using (var context = new EnglishAuctionEntities())
                {
                    var lsp = context.LoaiSanPhams.Where(p => p.Id == id).Single();
                    context.LoaiSanPhams.Remove(lsp);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;

        }

        public static bool Update(LoaiSanPham loaiSanPham)
        {
            bool result = true;
            try
            {
                using (var context = new EnglishAuctionEntities())
                {
                    var lsp = context.LoaiSanPhams.Where(p => p.Id == loaiSanPham.Id).Single();
                    lsp.MaLoai = loaiSanPham.MaLoai;
                    lsp.TenLoaiSanPham = loaiSanPham.TenLoaiSanPham;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;

        }

        public static bool Insert(LoaiSanPham loaiSanPham)
        {
            bool result = true;
            try
            {
                using (var context = new EnglishAuctionEntities())
                {
                    context.LoaiSanPhams.Add(loaiSanPham);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;

        }

        public static List<LoaiSanPham> GetAll()
        {
            List<LoaiSanPham> loaiSanPhams = new List<LoaiSanPham>();

            using (EnglishAuctionEntities context = new EnglishAuctionEntities())
            {
                loaiSanPhams = (from n in context.LoaiSanPhams 
                                //where n.MaLoai.Equals("AP01")
                                select n).ToList();
                //loaiSanPhams = context.LoaiSanPhams.ToList();
            }
            
            return loaiSanPhams;
        }
    }
}